package nl.infodation.emr.pricingservice.ui.page;

import java.io.Serializable;
import java.util.List;

import nl.infodation.emr.pricingservice.entity.SystemValue;

import com.github.peholmst.i18n4vaadin.I18N;
import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;

public class SessionData implements TransactionListener, Serializable , Cloneable {
	
	private static final long serialVersionUID = 1L;

	private I18N i18n;
	
	private Integer environment;
	
	private String username;
	
	private String roleName;
	
	private Integer roleId;
	
	private String deploymentRoleName;
	
	private String roleNameForRefData;
	
	private String version;
	
	private Integer Language;
	
	private boolean superAdmin;
	
	private boolean normalAdmin;
	
	 
	private  List<SystemValue> countriesToSee;
	
	private Application app; // For distinguishing between apps
	
	private static ThreadLocal<SessionData> instance =
		new ThreadLocal<SessionData>();
	
	public SessionData(Application app) {
	 this.app = app;
	
	 // It's usable from now on in the current request
	 instance.set(this);
	}
	
	@Override
	public void transactionStart(Application application,
	                          Object transactionData) {
	 // Set this data instance of this application
	 // as the one active in the current thread. 
	 if (this.app == application)
	     instance.set(this);
	}
	
	@Override
	public void transactionEnd(Application application,
	                        Object transactionData) {
	 // Clear the reference to avoid potential problems
	 if (this.app == application) {
	     instance.set(null);
	 }
	 
	}
	
	public static Integer getRoleId() {
		return instance.get().roleId;
	}

	public static void setRoleId(Integer roleId) {
		instance.get().roleId = roleId;
	}
	
	public static I18N getI18N() {
		return instance.get().i18n;
	}

	public static void setI18N(I18N i18n) {
		if (i18n == null)
			return;
		
		instance.get().i18n = i18n;
	}
	
	public static Integer getEnvironment() {
		return instance.get().environment;
	}

	public static void setEnvironment(Integer environment) {
		instance.get().environment = environment;
	}
	
	public static String getRoleName() {
		return instance.get().roleName;
	}

	public static void setRoleName(String roleName) {
		if (roleName == null)
			return;
		
		instance.get().roleName = roleName;
	}
	
	public static String getVersion() {
		return instance.get().version;
	}

	public static void setVersion(String version) {
		if (version == null)
			return;
		
		instance.get().version = version;
	}
	
	public static String getDeploymentRoleName() {
		return instance.get().deploymentRoleName;
	}

	public static void setDeploymentRoleName(String deployRoleName) {
		if ( deployRoleName == null)
			return;
		
		instance.get().deploymentRoleName = deployRoleName;
	}
	
	public static String getRoleNameForRefData() {
		return instance.get().roleNameForRefData;
	}

	public static void setRoleNameForRefData(String roleName) {
		if ( roleName == null)
			return;
		
		instance.get().roleNameForRefData = roleName;
	}

	public static List<SystemValue> getCountriesToSee() {
		return instance.get().countriesToSee;
	}

	public static void setCountriesToSee(List<SystemValue> countriesToSee) {
		if ( countriesToSee == null)
			return;
		
		instance.get().countriesToSee = countriesToSee;
	}
	
	public static boolean isSuperAdmin() {
		return instance.get().superAdmin;
	}

	public static void setSuperAdmin(boolean superAdmin) {
		instance.get().superAdmin = superAdmin;
	}
	
	public static boolean isNormalAdmin() {
		return instance.get().normalAdmin;
	}

	public static void setNormalAdmin(boolean normalAdmin) {
		instance.get().normalAdmin = normalAdmin;
	}
	
	public static String getUsername() {
		return instance.get().username;
	}

	public static void setUsername(String username) {
		instance.get().username = username;
	}

	public static Integer getLanguage() {
		return instance.get().Language;
	}

	public static void setLanguage(Integer language) {
		instance.get().Language = language;
	}	
	
}